<template>
  <div class="login-container">   
    <div class="login-box">    
      <IEpLink/>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login-form">
        <!-- 用户名 -->
        <el-form-item prop="name">
          <el-input v-model="loginForm.name"  placeholder="请输入用户名"><template #prefix><IEpLink></IEpLink></template></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="pwd">
          <el-input type="password" v-model="loginForm.pwd"  placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="login-btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from "vue";

const { proxy } = getCurrentInstance();
const loginForm = reactive({
  name: "",
  pwd: "",
});
const loginFormRef = ref(null);
const loginFormRules = reactive({
  name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
const login = () => {
  loginFormRef.value.validate(async (valid) => {
    if (!valid) return;
    const { data: res } = await proxy.$axios.post("/login", {
      name: loginForm.name,
      pwd: loginForm.pwd,
    });
    if (res.code !== 0) return proxy.$message.error("登录失败");
    proxy.$message.success("登录成功");
    window.sessionStorage.setItem("token", res.token);
    proxy.$router.push("/home");
  });
};
const resetLoginForm = () => {
  loginFormRef.value.resetFields();
};

</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}

.login-box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login-form {
  position: absolute;
  bottom: 20px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.login-btns {
  display: flex;
  justify-content: flex-end;
}
</style>